local InnerDataManagerFW = require("innerdata.InnerDataManagerFW")
local InnerDataDefine = require("innerdata.Define")
local ActDefine = require("activity.Define")
local Msg = require("core.Msg")
local Grid = require("bag.Grid")
local OperateActFW = require("activity.OperateActFW")
local ItemDefine = require("bag.ItemDefine")
local QPoint = require("activity.operate.QPoint")
local OperateActChongzhiRank = require("activity.operate.ChongzhiRank")
local LvRank = require("rank.LvRankLogic")
local PataRank = require("rank.PataRankLogic")
local Human = require("core.Human")
local RoleDBLogic = require("role.RoleDBLogic")
local MailLogicFW =require("mail.MailLogicFW")
local LvExcel = require("excel.OperateActivity").lvRank
local PataExcel = require("excel.OperateActivity").pataRank

CHONGZHI_DRAW_RECORD = CHONGZHI_DRAW_RECORD or {}
CRIT_DRAW_RECORD = CRIT_DRAW_RECORD or {}
QPOINT_RECORD = QPOINT_RECORD or {}

function sendChongzhiRecord(human)
    local sendMsg = Msg.gc.GC_YUNYING_DRAW_RECORD
    sendMsg.info[0] = #CHONGZHI_DRAW_RECORD
    local cache = {}
    for i=1, #CHONGZHI_DRAW_RECORD do
        sendMsg.info[i].name = CHONGZHI_DRAW_RECORD[i].name
        Grid.create(cache, CHONGZHI_DRAW_RECORD[i].id, CHONGZHI_DRAW_RECORD[i].cnt)
        Grid.makeItem(cache, sendMsg.info[i].item, 0, human)
    end
    sendMsg.id = ActDefine.OPERATEACT_TYPE_CHONGZHIDRAW
    Msg.send(sendMsg, human.fd)
end

function sendCritRecord(human)
    local sendMsg = Msg.gc.GC_YUNYING_DRAW_RECORD
    sendMsg.info[0] = #CRIT_DRAW_RECORD
    local cache = {}
    for i=1, #CRIT_DRAW_RECORD do
        sendMsg.info[i].name = CRIT_DRAW_RECORD[i].name
        Grid.create(cache, CRIT_DRAW_RECORD[i].id, CRIT_DRAW_RECORD[i].cnt)
        Grid.makeItem(cache, sendMsg.info[i].item, 0, human)
    end
    sendMsg.id = ActDefine.OPERATEACT_TYPE_CRITDRAW
    Msg.send(sendMsg, human.fd)
end

function sendQPointRecord(human)
    local sendMsg = Msg.gc.GC_YUNYING_DRAW_RECORD
    sendMsg.info[0] = #QPOINT_RECORD
    local cache = {}
    for i=1, #QPOINT_RECORD do
        sendMsg.info[i].name = QPOINT_RECORD[i].name
        Grid.create(cache, ItemDefine.ITEM_ID_YUANBAO, QPOINT_RECORD[i].cnt)
        Grid.makeItem(cache, sendMsg.info[i].item, 0, human)
    end
    sendMsg.id = ActDefine.OPERATEACT_TYPE_QPOINT
    Msg.send(sendMsg, human.fd)
end

function sendRecord(human, oType, info)
    if info.type == ActDefine.OPERATEACT_TYPE_CHONGZHIDRAW then
        sendChongzhiRecord(human)
    elseif info.type == ActDefine.OPERATEACT_TYPE_CRITDRAW  then
        sendCritRecord(human)
    elseif info.type == ActDefine.OPERATEACT_TYPE_QPOINT then
        sendQPointRecord(human)
    end
end

function setRecord(human, oType, info)
    local record = {}
    local len = 0
    if info.type == ActDefine.OPERATEACT_TYPE_CHONGZHIDRAW then
        record = CHONGZHI_DRAW_RECORD
        len = math.min(#record, 4)
    elseif info.type == ActDefine.OPERATEACT_TYPE_CRITDRAW  then
        record = CRIT_DRAW_RECORD
        len = math.min(#record, 14)
    elseif info.type == ActDefine.OPERATEACT_TYPE_QPOINT then
        record = QPOINT_RECORD
        len = math.min(#record, 5)
    end
    for i=len, 1, -1 do
        record[i + 1] = record[i]
    end
    record[1] = {}
    record[1].name = human.name
    record[1].id = info.id
    record[1].cnt = info.cnt
--    if info.type == ActDefine.OPERATEACT_TYPE_CHONGZHIDRAW then
--        sendChongzhiRecord(human)
--    elseif info.type == ActDefine.OPERATEACT_TYPE_CRITDRAW then
--        sendCritRecord(human)
--    elseif info.type == ActDefine.OPERATEACT_TYPE_QPOINT then
--        sendQPointRecord(human)
--    end
end

function onQPointEnd()
    QPoint.closeActivity()
end

function buyLottery(human)
    return QPoint.getReward(human)
end

function buyDailyGift(human)  
    if human.db.dailyGiftStatus == 0 then
      human.db.dailyGiftStatus = 1
      InnerDataManagerFW.Send2LInnerData(human, InnerDataDefine.INNERDATA_TYPE_DAILY_GIFT)
    end
end

function onGetChongzhiRankData(human, type)
    OperateActChongzhiRank.onProecss(human, human._id, type)
end

function onChongzhiCalc()
    OperateActChongzhiRank.onCalc()
end

function clearRecord()
  CHONGZHI_DRAW_RECORD = {}
  CRIT_DRAW_RECORD = {}
  QPOINT_RECORD = {}
end
function checkLvCanGet()
  local activityNum = OperateActFW.onGetCurActRuleID(ActDefine.OPERATEACT_TYPE_LVRANK)
  local activity = LvExcel[activityNum]
  if not activity then
    return
  end
  for i=1, #LvRank.LEVEL_RANK do
    for j=1, #activity.condition do
      if i <= activity.condition[j] then
        if i == 1 then
          local info = {}
          info.type = ActDefine.OPERATEACT_TYPE_LVRANK
          local db = RoleDBLogic.getDbByUuid(LvRank.LEVEL_RANK[i].uuid)
          info.sex = db.sex
          info.name = LvRank.LEVEL_RANK[i].name
          InnerDataManagerFW.Send2LInnerData(nil, InnerDataDefine.INNERDATA_TYPE_YUNYING_RANK, info)
        end
        local nGridList = {}
        for k=1, #activity.reward[j] do          
          nGridList[k] = Grid.create(nil, activity.reward[j][k][1], activity.reward[j][k][2], 1)
        end
        local str = string.format("恭喜您！在活动【等级排名】中获得了第%d名，特发此奖励。", i)
        MailLogicFW.addSystemMessage(LvRank.LEVEL_RANK[i].uuid, "等级排名", str, nGridList)
        break
      end
    end
  end
end
function checkPataCanGet()  
  local activityNum = OperateActFW.onGetCurActRuleID(ActDefine.OPERATEACT_TYPE_PATARANK)
  local activity = PataExcel[activityNum]
  if not activity then
    return
  end
  
  for i=1, #PataRank.PATA_RANK do
    for j=1, #activity.condition do
      if i <= activity.condition[j] then
        if i == 1 then
          local info = {}
          info.type = ActDefine.OPERATEACT_TYPE_LVRANK
          local db = RoleDBLogic.getDbByUuid(PataRank.PATA_RANK[i].uuid)
          info.sex = db.sex
          info.name = PataRank.PATA_RANK[i].name
          InnerDataManagerFW.Send2LInnerData(nil, InnerDataDefine.INNERDATA_TYPE_YUNYING_RANK, info)
        end
        local nGridList = {}
        for k=1, #activity.reward[j] do
          nGridList[k] = Grid.create(nil, activity.reward[j][k][1], activity.reward[j][k][2], 1)
        end
        local str = string.format("恭喜您！在活动【过关斩将】中一往无前成功勇夺第%d名，特发次奖励。", i)
        MailLogicFW.addSystemMessage(PataRank.PATA_RANK[i].uuid, "过关斩将", str, nGridList)
        break
      end
    end
  end
end

InnerDataManagerFW.Register(InnerDataDefine.INNERDATA_TYPE_YUNYING_RECORD, sendRecord)
OperateActFW.onRegisterCallBack(ActDefine.OPERATEACT_TYPE_QPOINT, nil, nil, onQPointEnd, nil)
